﻿Module modLogging
    Public Enum LogEventType
        Informasi
        Peringatan
        Kesalahan
    End Enum

    Public Sub WriteLog(tipe As LogEventType, message As String, Optional errObj As Exception = Nothing)
        Dim sb As New System.Text.StringBuilder
        With sb
            .AppendLine("==================================================")
            .AppendLine("Tipe  : " & tipe.ToString & ".")
            .AppendLine("Waktu : " & Now.ToLocalTime & ".")
            .AppendLine("Pesan : ")
            .AppendLine(message)
            If errObj Is Nothing Then
                .AppendLine("Error : Tidak ada.")
            Else
                .AppendLine("Error : " & IIf(errObj Is Nothing, "Ada.", "Tidak ada."))
                .AppendLine("--------------------------------------------------")
                .AppendLine(errObj.StackTrace)
                .AppendLine("--------------------------------------------------")
            End If
        End With
        Try
            Dim tulis As New IO.StreamWriter(Application.StartupPath & "\log\" & Now.ToString("ddMMyyyy") & ".txt")
            tulis.Write(sb.ToString)
            tulis.Close()
        Catch ex As Exception
            Debug.Print("Gagal menulis LOG. Pesan error : " & ex.Message)
        End Try
    End Sub
End Module
